package org.cn.osiris.org.service.impl;

import org.cn.osiris.basic.service.impl.BaseServiceImpl;
import org.cn.osiris.basic.util.Md5Utils;
import org.cn.osiris.basic.util.StrUtils;
import org.cn.osiris.org.domain.Employee;
import org.cn.osiris.org.mapper.EmployeeMapper;
import org.cn.osiris.org.service.IEmployeeService;
import org.cn.osiris.user.domain.Logininfo;
import org.cn.osiris.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    /**
     *同步操作
     * 并且对密码进行加盐加密
     *
     */

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void add(Employee employee) {
        //处理密码的严值
        String salt = StrUtils.getComplexRandomString(32);
        //对加盐后的密码进行加密
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);

        //设置加盐加密后的密码
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //管理员
        logininfo.setType(0);
        logininfo.setDisable(true);
        //获取到自增ID
        logininfoMapper.add(logininfo);


        //设置自增长id
        employee.setLogininfoId(logininfo.getId());
        super.add(employee);

        //添加角色
        employeeMapper.addEmployeeRole(employee);
    }

    @Override
    public void update(Employee employee) {
        //处理密码的严值
        String salt = StrUtils.getComplexRandomString(32);
        //对加盐后的密码进行加密
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);

        //设置加盐加密后的密码
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.findById(employee.getId());//loginfo_id

        //修改user信息
        employee.setLogininfoId(oldEmployee.getLogininfoId());
        employee.setShopId(oldEmployee.getShopId());
        super.update(employee);



        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.findById(oldEmployee.getLogininfoId());
        BeanUtils.copyProperties(employee,oldLogininfo);
        logininfoMapper.update(oldLogininfo);


        //先删除在添加

        //删除中间表信息
        employeeMapper.removeEmployeeRoleByEmployeeId(employee.getId());
        //保存新的信息
        employeeMapper.addEmployeeRole(employee);
    }

    @Override
    public void delete(Long id) {
        Employee employee = employeeMapper.findById(id);
        //删除登录信息表数据
        logininfoMapper.delete(employee.getLogininfoId());

        /*删除t_employee:删除中间表信息：t_employee_role*/
        employeeMapper.removeEmployeeRoleByEmployeeId(id);

        //删除employee表数据
        super.delete(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }


    //    private Logininfo employee2Logininfo(Employee employee) {
//        Logininfo logininfo = new Logininfo();
//
//        BeanUtils.copyProperties(employee,logininfo);
//        //当前对象是店铺管理员 type：0
//        logininfo.setType(0);
//        return logininfo;
//    }
}
